<?php
/**
 * Created by PhpStorm.
 * User: huangkai
 * Date: 2017/11/20
 * Time: 15:59
 */

namespace User\Controller;


use Think\Controller;

class StatisticsController extends Controller
{
    public static $api_arr = [
        "meeting",
        "lottery",       //大转盘
        "daily_sign"     //心情日历签
    ];//---功能块列表---

    public function index()
    {
        set_time_limit(0);
        $date = date("Y-m-d");
        foreach (self::$api_arr as $api) {
            $s_data_arr = S($api);
            if ($api == "meeting") {
                $cond_api['start_date'] = array("LT", $date . " 23:59:59");
                $cond_api['end_date'] = array("GT", $date . " 00:00:00");
                $cond_api['status'] = 1;
                $api_results = M($api)->where($cond_api)->select();
            }
            //大转盘
            if ($api == "lottery") {
                $where_lottery['keyword'] = $api;
                $where_lottery['startdate'] = array("LT", $date . " 23:59:59");
                $where_lottery['enddate'] = array("GT", $date . " 00:00:00");
                $where_lottery['status'] = 1;
                $api_results = M('redpacket')->where($where_lottery)->select();
            }
            //心情日历签
            if ($api == "daily_sign") {
                $where_daily_sign['status'] = 1;
                $api_results = M('daily_sign')->where($where_daily_sign)->select();
            }
            if (!empty($api_results)) {
                foreach ($api_results as $api_result) {
                    $data['pv_num'] = 0;
                    $data['uv_num'] = 0;
                    if ($api == "meeting") {
                        if (isset($s_data_arr[$api_result['meet_id']])) {
                            $data['pv_num'] = $s_data_arr[$api_result['meet_id']]['pv'];
                            $data['uv_num'] = $s_data_arr[$api_result['meet_id']]['uv'];
                        }
                        $data['topic_id'] = $api_result['meet_id'];
                    }
                    //大转盘
                    if ($api == "lottery") {
                        if (isset($s_data_arr[$api_result['id']])) {
                            $data['pv_num'] = $s_data_arr[$api_result['id']]['pv'];
                            $data['uv_num'] = $s_data_arr[$api_result['id']]['uv'];
                        }
                        $data['topic_id'] = $api_result['id'];
                    }
                    //心情日历签
                    if ($api == "daily_sign") {
                        if (isset($s_data_arr[$api_result['id']])) {
                            $data['pv_num'] = $s_data_arr[$api_result['id']]['pv'];
                            $data['uv_num'] = $s_data_arr[$api_result['id']]['uv'];
                        }
                        $data['topic_id'] = $api_result['id'];
                    }
                    $data['date'] = date("Y-m-d");
                    $data['month'] = date("m");
                    $data['year'] = date("Y");
                    $data['api'] = $api;
                    $data['status'] = 1;

                    //--查看是否有记录，有则相加，无则新建--
                    $cond['api'] = $api;
                    $cond['status'] = 1;
                    $cond['date'] = date("Y-m-d");
                    if ($api == "meeting") {
                        $cond['topic_id'] = $api_result['meet_id'];
                    }
                    if ($api == "lottery") {
                        $cond['topic_id'] = $api_result['id'];
                    }
                    if ($api == "daily_sign") {
                        $cond['topic_id'] = $api_result['id'];
                    }
                    $data_exist = M("statistics")->where($cond)->find();
                    if ($data_exist) {
                        $data['pv_num'] = $data_exist['pv_num'] + $data['pv_num'];
                        $data['uv_num'] = $data_exist['uv_num'] + $data['uv_num'];
                        M("statistics")->where(['id' => $data_exist['id']])->save($data);
                    } else {
                        M("statistics")->add($data);
                    }
                }
            }
            S($api, NULL);
        }
        $this->set_yuandan(1);
        $this->set_wnz(1);
        $this->set_viewpage();
        $this->jt_data();
    }

    public function set_yuandan($topic_id = 1)
    {
        $s_yuandan = S("yuandan");
        $data['pv_num'] = 0;
        $data['uv_num'] = 0;
        if (isset($s_yuandan[$topic_id])) {
            $data["pv_num"] = $s_yuandan[$topic_id]['pv'];
            $data["uv_num"] = $s_yuandan[$topic_id]['uv'];
        }
        $data['date'] = date("Y-m-d");
        $data['month'] = date("m");
        $data['year'] = date("Y");
        $data['api'] = "yuandan";
        $data['status'] = 1;
        $data["topic_id"] = $topic_id;

        $cond['api'] = "yuandan";
        $cond['status'] = 1;
        $cond['date'] = date("Y-m-d");
        $cond['topic_id'] = $topic_id;
        $data_exist = M("statistics")->where($cond)->find();
        if ($data_exist) {
            $data['pv_num'] = $data_exist['pv_num'] + $data['pv_num'];
            $data['uv_num'] = $data_exist['uv_num'] + $data['uv_num'];
            M("statistics")->where(['id' => $data_exist['id']])->save($data);
        } else {
            M("statistics")->add($data);
        }
        S("yuandan", NULL);
    }

    public function set_wnz($topic_id = 1)
    {
        $s_wnz = S("wnz");
        $data['pv_num'] = 0;
        $data['uv_num'] = 0;
        if (isset($s_wnz[$topic_id])) {
            $data["pv_num"] = $s_wnz[$topic_id]['pv'];
            $data["uv_num"] = $s_wnz[$topic_id]['uv'];
        }
        $data['date'] = date("Y-m-d");
        $data['month'] = date("m");
        $data['year'] = date("Y");
        $data['api'] = "wnz";
        $data['status'] = 1;
        $data["topic_id"] = $topic_id;

        $cond['api'] = "wnz";
        $cond['status'] = 1;
        $cond['date'] = date("Y-m-d");
        $cond['topic_id'] = $topic_id;
        $data_exist = M("statistics")->where($cond)->find();
        if ($data_exist) {
            $data['pv_num'] = $data_exist['pv_num'] + $data['pv_num'];
            $data['uv_num'] = $data_exist['uv_num'] + $data['uv_num'];
            M("statistics")->where(['id' => $data_exist['id']])->save($data);
        } else {
            M("statistics")->add($data);
        }
        S("wnz", NULL);
    }

    //统计H5等页面的访问量pv、访问人数uv
    public function set_viewpage()
    {
        $ids = S('h5_view_page_ids');
        if ($ids) {
            foreach ($ids as $id) {
                $cache = S('h5_view_page_cache_' . $id);
                $where['id'] = $id;
                $data = M('data_important')->where($where)->find();
                if (!empty($data)) {
                    $pv = $cache['pv'] ? $cache['pv'] : 0;
                    $uv = $cache['uv'] ? $cache['uv'] : 0;
                    $pv = $pv + $data['rensheng_view'];
                    $uv = $uv + $data['rensheng_like'];
                    M('data_important')->where($where)->save(array('rensheng_view' => $pv, 'rensheng_like' => $uv));
                    S('h5_view_page_cache_' . $id, NULL);
                } else {
                    $data['rensheng_view'] = $cache['pv'];
                    $data['rensheng_like'] = $cache['uv'];
                    $data['id'] = $id;
                    M('data_important')->add($data);
                }
            }
        }
        S('h5_view_page_ids', NULL);
    }

    public function jt_data()
    {
        $user_counts = M("jt_user_clock")->group("user_id,new_id")->field("user_id,new_id,count(*) as count")->order("count desc")->limit(2000)->select();
        foreach ($user_counts as $user_count) {
            if ($user_count['count'] > 1) {
                $cond_user_clock['user_id'] = $user_count['user_id'];
                $cond_user_clock['new_id'] = $user_count['new_id'];
                $user_clock = M("jt_user_clock")->where($cond_user_clock)->find();
                $cond_del['user_id'] = $user_count['user_id'];
                $cond_del['new_id'] = $user_count['new_id'];
                $cond_del['id'] = array("NEQ", $user_clock['id']);
                if (!empty($cond_del['id'])) {
                    M()->startTrans();
                    $del_count = M("jt_user_clock")->where($cond_del)->count();
                    $res1 = M("jt_user_clock")->where($cond_del)->delete();

                    $cond_user_total_update['user_id'] = $user_count['user_id'];
                    $cond_user_total_update['type'] = $user_clock['type'];
                    $res2 = M("jt_user_clock_total")->where($cond_user_total_update)->setDec("clock", $del_count);

                    $cond_user['id'] = $user_count['user_id'];
                    $user = M("jt_user")->where($cond_user)->find();
                    $cond_dept_up['dept_id'] = $user['dept_id'];
                    $cond_dept_up['new_id'] = $user_clock['new_id'];
                    $res3 = M("jt_dept_data")->where($cond_dept_up)->setDec("clock", $del_count);
                    if(!empty($res1) && !empty($res2) && !empty($res3)){
                        M()->commit();
                    }else{
                        M()->rollback();
                    }
                }
            } else {
                break;
            }
        }

        //------统计用户的打卡文章总数-----
        $cond_nb_max['type'] = 0;
        $cond_nb_max['clock'] = 1;
        $cond_nb_max['status'] = array("NEQ", 9);
        $nb_clock_max = M("jt_news")->where($cond_nb_max)->count();

        $cond_wx_max['type'] = 1;
        $cond_wx_max['clock'] = 1;
        $cond_wx_max['status'] = array("NEQ", 9);
        $wx_clock_max = M("jt_news")->where($cond_wx_max)->count();


        //------阅读打卡所有员工----
        $cond_user_id_arr['status'] = 1;
        $cond_user_id_arr['type'] = 0;
        $user_id_arr = M("jt_user_clock_total")->where($cond_user_id_arr)->order("clock desc,user_id asc")->field("user_id")->select();
        S("user_nb_clock_all", $user_id_arr);

        //-----设置用户阅读排行榜----
        $cond['type'] = 0;
        $cond['status'] = 1;
        $user_list = M("jt_user_clock_total")->where($cond)->order("clock desc,user_id asc")->limit(0, 100)->field("user_id, clock")->select();
        foreach ($user_list as &$user_nb) {
            if ($user_nb['clock'] > $nb_clock_max) {
                $user_nb['clock'] = $nb_clock_max;
            }
        }
        S("user_nb_clock", $user_list);

        //------阅读打卡所有员工----
        $cond_user_id_arr['status'] = 1;
        $cond_user_id_arr['type'] = 1;
        $user_id_arr = M("jt_user_clock_total")->where($cond_user_id_arr)->order("clock desc,user_id asc")->field("user_id")->select();
        S("user_wx_clock_all", $user_id_arr);

        //-----设置用户文章排行榜----
        $cond['type'] = 1;
        $cond['status'] = 1;
        $user_list1 = M("jt_user_clock_total")->where($cond)->order("clock desc,user_id asc")->limit(0, 100)->field("user_id, clock")->select();
        foreach ($user_list1 as &$user_wx) {
            if ($user_wx['clock'] > $wx_clock_max) {
                $user_wx['clock'] = $wx_clock_max;
            }
        }
        S("user_wx_clock", $user_list1);


        $this->jt_dept_data(0);//---部门阅读打卡---
        $this->jt_dept_data(1);//---部门文章打卡---
        $this->jt_dept_data_per(0);//---部门阅读打卡率---
        $this->jt_dept_data_per(1);//---部门文章打卡率---
    }

    private function jt_dept_data($type = 0)
    {
        $cond['type'] = $type;
        $cond['status'] = 1;
        $start_date = date('Y-01-01 00:00:00');
        $end_date = date("Y-m-d H:i:s");
        $cond['add_time'] = array("between", array($start_date, $end_date));
        $dept_data_list_my = array();

        $count = M("jt_dept_data")->where($cond)->count();
        $number = $count / 100;
        for ($i = 0; $i <= $number; $i++) {
            $dept_data_list = M("jt_dept_data")->where($cond)->field("dept_id,number,clock")->limit($i * 100, 100)->select();
            //------部门统计数据整合--------
            foreach ($dept_data_list as $dept_data) {
                if (empty($dept_data_list_my[$dept_data['dept_id']])) {
                    $dept_data_list_my[$dept_data['dept_id']] = array("dept_id" => $dept_data['dept_id']);
                }
                if ($dept_data['clock'] > $dept_data['number']) {
                    $dept_data['clock'] = $dept_data['number'];
                }
                $dept_data_list_my[$dept_data['dept_id']]['number'] += $dept_data['number'];
                $dept_data_list_my[$dept_data['dept_id']]['clock'] += $dept_data['clock'];
            }
        }
        //-----按打卡次数倒叙排列----
        $flag = array();
        foreach ($dept_data_list_my as $dept_data_my1) {
            $flag[] = $dept_data_my1['clock'];
        }
        array_multisort($flag, SORT_DESC, $dept_data_list_my);
        /*if (count($dept_data_list_my) > 20) {
            $dept_data_list_my = array_slice($dept_data_list_my, 0, 20);
        }*/
        //---根据截取的部门数据获取部门名称---
        $dept_id_arr = [];
        foreach ($dept_data_list_my as $dept_data_my) {
            $dept_id_arr[] = $dept_data_my['dept_id'];
        }
        if (!empty($dept_id_arr)) {
            $cond_dept_id_arr['id'] = array("IN", $dept_id_arr);
            $depts = M("jt_dept")->where($cond_dept_id_arr)->getField("id,name");
            foreach ($dept_data_list_my as &$dept_data_my) {
                $dept_data_my['dept_name'] = $depts[$dept_data_my['dept_id']];
            }
        }
        if (empty($type)) {
            S("dept_data_dept_clock_nb", $dept_data_list_my);
        } else {
            S("dept_data_dept_clock_wx", $dept_data_list_my);
        }
    }

    private function jt_dept_data_per($type = 0)
    {
        $cond['type'] = $type;
        $cond['status'] = 1;
        $start_date = date('Y-01-01 00:00:00');
        $end_date = date("Y-m-d H:i:s");
        $cond['add_time'] = array("between", array($start_date, $end_date));
        $dept_data_list_my = array();

        $count = M("jt_dept_data")->where($cond)->count();
        $number = $count / 100;
        for ($i = 0; $i <= $number; $i++) {
            $dept_data_list = M("jt_dept_data")->where($cond)->field("dept_id,number,clock")->limit($i * 100, 100)->select();
            //------部门统计数据整合--------
            foreach ($dept_data_list as $dept_data) {
                if (empty($dept_data_list_my[$dept_data['dept_id']])) {
                    $dept_data_list_my[$dept_data['dept_id']] = array("dept_id" => $dept_data['dept_id']);
                }
                if ($dept_data['clock'] > $dept_data['number']) {
                    $dept_data['clock'] = $dept_data['number'];
                }
                $dept_data_list_my[$dept_data['dept_id']]['number'] += $dept_data['number'];
                $dept_data_list_my[$dept_data['dept_id']]['clock'] += $dept_data['clock'];
            }
        }
        //-----计算部门完成率-----
        foreach ($dept_data_list_my as &$dept_data_my) {
            if ($dept_data_my['number'] == 0) {
                $dept_data_my['per'] = 0;
            } else {
                $dept_data_my['per'] = number_format($dept_data_my['clock'] * 100 / $dept_data_my['number'], 2);
                if ($dept_data_my['per'] > 100.00) {
                    $dept_data_my['per'] = 100.00;
                }
            }
        }

        //-----按完成率倒叙排列----
        $flag = array();
        foreach ($dept_data_list_my as $dept_data_my1) {
            $flag[] = $dept_data_my1['per'];
        }
        array_multisort($flag, SORT_DESC, $dept_data_list_my);
        /*if (count($dept_data_list_my) > 20) {
            $dept_data_list_my = array_slice($dept_data_list_my, 0, 20);
        }*/
        //---根据截取的部门数据获取部门名称---

        $dept_id_arr = [];
        foreach ($dept_data_list_my as $dept_data_my_id) {
            $dept_id_arr[] = $dept_data_my_id['dept_id'];
        }
        if (!empty($dept_id_arr)) {
            $cond_dept_id_arr['id'] = array("IN", $dept_id_arr);
            $depts = M("jt_dept")->where($cond_dept_id_arr)->getField("id,name");
            foreach ($dept_data_list_my as &$dept_data_my) {
                $dept_data_my['dept_name'] = $depts[$dept_data_my['dept_id']];
            }
        }
        if (empty($type)) {
            S("dept_dataClockPerNb", $dept_data_list_my);
        } else {
            S("dept_dataClockPerWx", $dept_data_list_my);
        }
    }
}